﻿using Mate.Caching;
using Mate.Db.Model.Comment;
using Mate.Db.Model.Users;
using Mate.Db.SqlSugar;
using Mate.Dto.Comment;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace Mate.Comment.Service
{
    public class ReplyService : DataService<mp_reply>, IReplyService
    {
        private readonly ICacheManager _cache;
        public ReplyService(DataClient dataClient, ICacheManager cache) : base(dataClient)
        {
            this._cache = cache;
        }

        #region 获取评论列表
        /// <summary>
        /// 获取评论列表
        /// </summary>
        /// <param name="commentid">点评id</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="key">搜索关键字</param>
        /// <param name="totalCount">总条数</param>
        /// <returns></returns>
        public IList<ReplyModel> GetReplyList(int commentid,int pageIndex, int pageSize, string key, ref int totalCount)
        {
            string likecount = "(select count(1) from mp_like where userid=r.userid and relateid=r.id and liketype=1 and is_delete=1) likecount";

            return dataClient.Queryable<mp_reply,mp_user>((r,u)=>new object[]{
                JoinType.Left,r.userid==u.id
                })
                 .Where(r => r.is_delete == 1&&r.commentid==commentid)
                 .WhereIF(!string.IsNullOrWhiteSpace(key), r =>r.content.Contains(key))
                 .Select<ReplyModel>($"r.*,u.username,{likecount}")
                 .ToPageList(pageIndex, pageSize, ref totalCount);
        }
    }
    #endregion

}
